package net.itzyz.phone;

import net.itzyz.util.ToDoDB;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteCursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class DailyActivity extends Activity {
	
	private ToDoDB toDoDB;
	private Cursor myCursor;
	private ListView myListView;
	private EditText myEditText;
	private int _id;
	private final static int MENU_ADD = Menu.FIRST;
	private final static int MENU_EDIT = Menu.FIRST+1;
	private final static int MENU_DELETE = Menu.FIRST+2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.daily);
		myListView = (ListView)findViewById(R.id.myListView);
		myEditText = (EditText)findViewById(R.id.myEditText);
		
		toDoDB = new ToDoDB(this);
		myCursor = toDoDB.select();
		
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.list,myCursor,
				new String[] {ToDoDB.FIELD_TEXT},
				new int[]{R.id.title});
		myListView.setAdapter(adapter);
		myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			public void onItemClick(AdapterView<?> adp, View v, int index, long arg3) {
				myCursor.move(index);
				_id = myCursor.getInt(0);
				myEditText.setText(myCursor.getString(1));
			}
			
		});
		myListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> adp, View v, int index, long arg3) {
				SQLiteCursor sc = (SQLiteCursor) adp.getSelectedItem();
				_id = sc.getInt(0);
				myEditText.setText(sc.getString(1));
			};

			public void onNothingSelected(AdapterView<?> arg0) {
				
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(Menu.NONE, MENU_ADD, 0, R.string.strAddButton);
		menu.add(Menu.NONE, MENU_EDIT, 0, R.string.strEditButton);
		menu.add(Menu.NONE, MENU_DELETE, 0, R.string.strDeleteButton);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch(item.getItemId()){
		case MENU_ADD:
			addToDo();
			break;
		case MENU_EDIT:
			editToDo();
			break;
		case MENU_DELETE:
			deleteToDo();
			break;
		}
		return true;
	}
	
	private void addToDo(){
		String text = myEditText.getText().toString();
		if(!"".equals(text)){
			toDoDB.insert(text);
			myCursor.requery();
			myListView.invalidateViews();
			myEditText.setText("");
			_id=0;
		}
	}
	
	private void editToDo(){
		String text = myEditText.getText().toString();
		if(!"".equals(text)){
			toDoDB.update(_id, text);
			myCursor.requery();
			myListView.invalidateViews();
			myEditText.setText("");
			_id=0;
		}
	}
	
	private void deleteToDo(){
		String text = myEditText.getText().toString();
		if(_id==0)return;
		if(!"".equals(text)){
			toDoDB.delete(_id);
			myCursor.requery();
			myListView.invalidateViews();
			myEditText.setText("");
			_id=0;
		}
	}
}
